// pages/order/createOrder.js

const app = getApp();
let api = require('../../../utils/api.js');
let qqMapWx = require('../../../utils/qqmap-wx-jssdk.min')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    systemMoney: null,
    fromProvince: '',
    fromCity: '',
    fromAddress: '',
    toProvince: '',
    toCity: '',
    toAddress: '',
    freightBulk: {},
    freightWeight: {},
    area: '',
    fromAdd: "",
    fromNumber: '',
    str: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight - app.globalData.navHeight
        });
      }
    });
  },
  /*
   * 发布货源
   */
  createNewOrder: function (e) {
    // 显示遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
      showCarModelStatus: true
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export()
      });
    }.bind(this), 200)
  },
  //隐藏对话框
  hideModal: function () {
    // 隐藏遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
      keyboardHeight: 0
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export(),
        showCarModelStatus: false,
        keyboardHeight: 0
      })
    }.bind(this), 200)
  },
  // 获取键盘高度
  keyBoardHeight: function (e) {
    this.setData({
      keyboardHeight: e.detail.height
    });
  },
  // 离开出发地
  leaveLocation: function (e) {
    var that = this;
    // 实例化腾讯地图SDK
    var qqmapsdk = new qqMapWx({
      key: 'NT2BZ-Y5Q34-YYCUF-DLQNZ-KW3L5-TCFCC' // 必填
    });
    wx.chooseLocation({
      success: (res) => {
        console.log(res);
        let latitude = res.latitude;
        let longitude = res.longitude;
        qqmapsdk.reverseGeocoder({
          location: {
            latitude,
            longitude
          },
          success(res) {
            console.log(res);
            let add = res.result.address_component
            that.setData({
              fromProvince: add.province,
              fromCity: add.city,
              fromAddress: add.district + add.street + add.street_number
            })
          },
          fail(err) {
            console.log(err)
            wx.showToast('获取城市失败')
          }
        })
      }
    });
  },
  // 到达目的地
  arriveLocation() {
    var that = this;
    // 实例化腾讯地图SDK
    var qqmapsdk = new qqMapWx({
      key: 'NT2BZ-Y5Q34-YYCUF-DLQNZ-KW3L5-TCFCC' // 必填
    });
    wx.chooseLocation({
      success: (res) => {
        console.log(res);
        let latitude = res.latitude;
        let longitude = res.longitude;
        qqmapsdk.reverseGeocoder({
          location: {
            latitude,
            longitude
          },
          success(res) {
            console.log(res);
            let add = res.result.address_component
            that.setData({
              toProvince: add.province,
              toCity: add.city,
              toAddress: add.district + add.street + add.street_number
            })
          },
          fail(err) {
            console.log(err)
            wx.showToast('获取城市失败')
          }
        })
      }
    });
  },
  /**
   * 监听输入地址
   */
  inputWacthAdress: function (e) {
    // item 等价于 fromAddress
    let item = e.currentTarget.dataset.model;
    this.setData({
      [item]: e.detail.value
    });
  },

  // 输入监听
  inputWacth: function (e) {
    let item = e.currentTarget.dataset.model + '.' + e.currentTarget.dataset.size;
    var money = '';
    if (/^(\d?)+(\.\d{0,1})?$/.test(e.detail.value)) { //正则验证，提现金额小数点后不能大于两位数字
      money = e.detail.value;
    } else {
      money = e.detail.value.substring(0, e.detail.value.length - 1);
      e.detail.value = e.detail.value.substring(0, e.detail.value.length - 1);
    }

    this.setData({
      [item]: money
    });
  },
  /**
   * 提交下一步
   */
  createNewOrderNext: function (e) {
    var that = this;

    if ((Object.keys(that.data.freightWeight).length === 0) || (that.data.useVehicleType) == undefined || (that.data.vehicleLen) == undefined || (that.data.vehicleModel) == undefined || that.data.vehicleLen.length == 0 || that.data.vehicleModel.length == 0 || that.data.useVehicleType.length == 0) {
      wx.showToast({
        title: '请输入完整信息后再进入下一步',
        icon: 'none',
      });
      return;
    } else {
      wx.navigateTo({
        url: 'createOrderNext?data=' + JSON.stringify(that.data)
      })
    }
  },
  confirmFreight: function (e) {
    var that = this;
    // console.log(that.data)

    if (that.data.freightWeight.max || that.data.freightWeight.min) {
      if ((that.data.freightBulk.min == 0 && !that.data.freightBulk.max) || (that.data.freightBulk.max == 0 && !that.data.freightBulk.min) || (that.data.freightWeight.min == 0 && !that.data.freightWeight.max) || (that.data.freightWeight.max == 0 && !that.data.freightWeight.min)) {

        wx.showToast({
          title: '请输入正确的范围',
          icon: 'none',
        });
        return;
      }

      if ((that.data.freightBulk.max && that.data.freightBulk.min) && ((parseInt(that.data.freightBulk.max) < parseInt(that.data.freightBulk.min)) || (that.data.freightBulk.max == 0 && that.data.freightBulk.min == 0))) {
        // console.log(that.data.freightBulk.min);

        wx.showToast({
          title: '请输入正确的体积范围',
          icon: 'none',
        });
        return;
      }
      if ((that.data.freightWeight.max && that.data.freightWeight.min) && ((parseInt(that.data.freightWeight.max) < parseInt(that.data.freightWeight.min)) || (that.data.freightWeight.max == 0 && that.data.freightWeight.min == 0))) {

        wx.showToast({
          title: '请输入正确的重量范围',
          icon: 'none',
        });
        return;
      }
      var freightBulk = that.data.freightBulk;
      var freightWeight = that.data.freightWeight;
      that.setData({
        freightBulk: freightBulk,
        freightWeight: freightWeight
      })

      that.hideModal();
    } else {

      wx.showToast({
        title: '重量为必填项，请至少输入一项重量',
        icon: 'none',
      });
    }

  }
})